package com.dubbo.provider.tomcat;

import com.alibaba.fastjson.JSONObject;
import com.dubbo.provider.entity.Invocation;
import com.dubbo.provider.map.RegisterMap;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: 石亚宁
 * @create: 2020-05-28 15:29
 **/

public class HttpServlethandler {
public 	void handler(ServletRequest req, ServletResponse res){
		try {
			res.setContentType("application/json;charset=utf-8");

			String jsons = req.getParameter("json");
			System.out.println(jsons);
			Invocation invocation = JSONObject.parseObject(jsons, Invocation.class);
			/*ServletInputStream inputStream = req.getInputStream();
			inputStream.read(jsons.getBytes());
			ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
			Invocation invocation= (Invocation) objectInputStream.readObject();*/
			Class aClass = RegisterMap.get(invocation.getInterfaceName());
			Method method = aClass.getMethod(invocation.getMethodName(), invocation.getParametersTypes());
			Object invoke =  method.invoke(aClass.newInstance(), invocation.getParameters());
			//IOUtils.write(invoke,res.getOutputStream());
			res.getWriter().print(invoke);
		} catch (Exception e) {
				e.printStackTrace();
			}


	}
}
